package com.example.ymm.modules1.service.impl.hl.kq;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.KqRule;
import com.example.ymm.modules1.mapper.my.user.KqRuleMapper;
import com.example.ymm.modules1.query.kq.KqRuleQueryCriteria;
import com.example.ymm.modules1.service.hl.kq.KqRuleService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class KqRuleServiceImpl extends BaseServiceImpl<KqRuleMapper, KqRule> implements KqRuleService {



    @Override
    public TableList<KqRule> list(KqRuleQueryCriteria criteria, YmPageDto pageDto) {
        IPage<KqRule> page = new Page<KqRule>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(KqRule.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<KqRule> tableList=new TableList(page);
        return tableList;
    }

    public List<KqRule> queryAll(KqRuleQueryCriteria criteria) {
        List<KqRule> result =baseMapper.selectList(QueryHelpPlus.getPredicate(KqRule.class, criteria));
        return result;
    }


    @Override
    public List<String> importRule(List<KqRule> list) {
        remove(null);
        List<String> result=new ArrayList<>();
        List<KqRule> adds=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            KqRule kqRule = list.get(i);
            if(UtilValidate.isEmpty(kqRule.getCheJianName())){
                result.add("第"+(i+1)+"行 车间名称不能为空");
            }
            if(UtilValidate.isEmpty(kqRule.getTypeValue())){
                result.add("第"+(i+1)+"行 规则类型不能为空");
            }else{
                if(kqRule.getTypeValue().equals("正常班")){
                    kqRule.setType(1);
                }else if(kqRule.getTypeValue().equals("连班")){
                    kqRule.setType(2);
                }
                else if(kqRule.getTypeValue().equals("加班")){
                    kqRule.setType(3);
                }else{
                    result.add("第"+(i+1)+"行 规则类型请输入：正常班，连班，加班 ");
                }
            }
            if(UtilValidate.isEmpty(kqRule.getShangwuUpTxt())){
                result.add("第"+(i+1)+"行 上午上班时间不能为空");
            }
            if(UtilValidate.isNotEmpty(kqRule.getShangwuUpTxt())){
                kqRule.setShangwuUpTxt(kqRule.getShangwuUpTxt().trim().replace("#", ":"));
            }

            if(UtilValidate.isNotEmpty(kqRule.getShangwuDownTxt())){
                kqRule.setShangwuDownTxt(kqRule.getShangwuDownTxt().trim().replace("#", ":"));
            }

            if(UtilValidate.isNotEmpty(kqRule.getZhongwuLimitTxt())){
                kqRule.setZhongwuLimitTxt(kqRule.getZhongwuLimitTxt().trim().replace("#", ":"));
            }
            if(UtilValidate.isNotEmpty(kqRule.getXiawuLimitTxt())){
                kqRule.setXiawuLimitTxt(kqRule.getXiawuLimitTxt().trim().replace("#", ":"));
            }
            if(UtilValidate.isNotEmpty(kqRule.getXiawuUpTxt())){
                kqRule.setXiawuUpTxt(kqRule.getXiawuUpTxt().trim().replace("#", ":"));
            }
            if(UtilValidate.isNotEmpty(kqRule.getXiawuDownTxt())){
                kqRule.setXiawuDownTxt(kqRule.getXiawuDownTxt().trim().replace("#", ":"));
            }
            if(UtilValidate.isNotEmpty(kqRule.getWanshangUpTxt())){
                kqRule.setWanshangUpTxt(kqRule.getWanshangUpTxt().trim().replace("#", ":"));
            }

            if(UtilValidate.isNotEmpty(kqRule.getWanshangDownTxt())){
                kqRule.setWanshangDownTxt(kqRule.getWanshangDownTxt().trim().replace("#", ":"));
            }
            if(UtilValidate.isNotEmpty(kqRule.getRemark())){
                kqRule.setRemark(kqRule.getRemark().trim());
            }
            adds.add(kqRule);
        }
        if(UtilValidate.isNotEmpty(result)){
            return result;
        }
        saveOrUpdateBatch(adds);
        return result;
    }
}
